package com.spring.sunflower.common

import android.Manifest
import android.animation.ValueAnimator
import android.content.Intent
import android.os.Handler
import android.view.View
import android.view.WindowManager
import android.view.animation.Animation
import android.view.animation.LinearInterpolator
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import com.spring.sunflower.MainActivity
import com.spring.sunflower.R
import com.spring.sunflower.base.BaseCompatActivity
import com.spring.sunflower.constant.Constant
import com.spring.sunflower.dialog.OnPermissionIntroClickListener
import com.spring.sunflower.dialog.PermissionIntroDialog
import com.spring.sunflower.util.DisplayUtils
import jp.wasabeef.glide.transformations.CropCircleTransformation

/**
 * Created by Kevin on 2021/8/8<br/>
 *
 * Describe:<br/>
 */
class LocationActivity : BaseCompatActivity() {
    private var rlHead: RelativeLayout? = null
    private var ivHead: ImageView? = null
    private var ivLocation: ImageView? = null
    private var tvDot: TextView? = null
    private val dotTexts = arrayOf(".", "..", "...")
    private val handler = Handler()
    private var animator01: ValueAnimator? = null
    private var animator02: ValueAnimator? = null
    private var animator03: ValueAnimator? = null
    private var isNeedGuide = false
    override fun getLayoutID(): Int = R.layout.activity_location
    override fun doSomethingBeforeOnCreate() {
        super.doSomethingBeforeOnCreate()
        val window = window
        window.setFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN
        )
        val params = window.attributes
        params.systemUiVisibility =
            View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_IMMERSIVE
        window.attributes = params
    }

    override fun initView() {
        rlHead = findViewById(R.id.head_layout)
        ivHead = findViewById(R.id.head_img)
        ivLocation = findViewById(R.id.locate_animate_img)
        tvDot = findViewById(R.id.dot_tv)
        val avatar: String = getField(Constant.KEY_PHOTO, "")
        Glide.with(this).load(avatar)
            .apply(RequestOptions.bitmapTransform(CropCircleTransformation()))
            .into(ivHead!!)
        startAnimation()
        handler.postDelayed(Runnable {
            showPermission()
        }, 1000)
    }

    private fun showPermission() {
        val granted = XXPermissions.isGranted(this, Manifest.permission.ACCESS_FINE_LOCATION)
        if (!granted) {
            val dialog = PermissionIntroDialog(
                this,
                R.layout.dialog_permission_intro_location,
                object : OnPermissionIntroClickListener {
                    override fun onClickCancel() {
                        goMain(1510)
                    }

                    override fun onClickOK() {
                        XXPermissions.with(this@LocationActivity)
                            .permission(Permission.ACCESS_FINE_LOCATION)
                            .request(object : OnPermissionCallback {
                                override fun onGranted(permissions: List<String>, all: Boolean) {
                                    goMain(1510)

                                }

                                override fun onDenied(permissions: List<String>, never: Boolean) {
                                    goMain(31010)
                                }
                            })
                    }
                })
            showPermissionIntroDialog(dialog)
        } else {
            goMain(3010)
        }
    }

    private fun goMain(delayMillis: Long) {
        handler.postDelayed(Runnable {
            cancelAnimation()
            val intent = Intent(this@LocationActivity, MainActivity::class.java)
            startActivity(intent)
            finish()
        }, delayMillis)
    }

    private fun startAnimation() {
        val displayWidth: Int = DisplayUtils.getScreenWidth(this)
        val margin: Int = displayWidth - DisplayUtils.dpTpx(this, 46)
        animator01 = ValueAnimator.ofInt(0, margin)
        animator01?.let {
            it.duration = 1000
            it.addUpdateListener(ValueAnimator.AnimatorUpdateListener { animation ->
                val currentValue = animation.animatedValue as Int
                val layoutParams = ivLocation!!.layoutParams as RelativeLayout.LayoutParams
                layoutParams.width = currentValue
                ivLocation!!.layoutParams = layoutParams
            })
            it.repeatMode = ValueAnimator.REVERSE
            it.interpolator = LinearInterpolator()
            it.repeatCount = Animation.INFINITE
            it.start()
        }
        val maxMargin: Int = DisplayUtils.dpTpx(this, 64)
        val minMargin: Int = DisplayUtils.dpTpx(this, 44)
        animator02 = ValueAnimator.ofInt(minMargin, maxMargin)
        animator02?.let {

            it.duration = 500
            it.addUpdateListener(ValueAnimator.AnimatorUpdateListener { animation ->
                val currentValue = animation.animatedValue as Int
                val layoutParams = rlHead!!.layoutParams as RelativeLayout.LayoutParams
                layoutParams.setMargins(0, currentValue, 0, 0)
                rlHead!!.layoutParams = layoutParams
            })
            it.repeatMode = ValueAnimator.REVERSE
            it.interpolator = LinearInterpolator()
            it.repeatCount = Animation.INFINITE
            it.start()
        }
        animator03 = ValueAnimator.ofInt(0, 3).setDuration(1000)
        animator03?.let {
            it.repeatCount = ValueAnimator.INFINITE
            it.addUpdateListener(ValueAnimator.AnimatorUpdateListener { animation ->
                val i = animation.animatedValue as Int
                tvDot!!.text = dotTexts[i % dotTexts.size]
            })
            it.start()
        }
    }


    override fun onDestroy() {
        super.onDestroy()
        cancelAnimation()
    }

    private fun cancelAnimation() {
        if (animator01 != null) {
            animator01!!.cancel()
            animator01 = null
        }
        if (animator02 != null) {
            animator02!!.cancel()
            animator02 = null
        }
        if (animator03 != null) {
            animator03!!.cancel()
            animator03 = null
        }
    }
}